<?php
namespace app\admin\controller;
use app\admin\model\User as userModel;
use think\Db;
use Env;
use PHPExcel_IOFactory;
class User extends Common
{
	function index(){
		$userModel = new userModel();
		$userData = $userModel->userData();
		$inputData = input('get.');

//		dump($userData);exit;
		$this->assign('list',$userData['list']);
		$this->assign('page',$userData['page']);
		$this->assign('count',$userData['count']);
		$this->assign('inputData',$inputData);

		return $this->fetch('index');
	}

	//会员列表
	function ajaxIndex(){
// 		$userModel = new userModel();
// 		$userData = $userModel->userData();

// //		dump($userData);exit;
// 		$this->assign('list',$userData['list']);
// 		$this->assign('page',$userData['page']);
// 		$this->assign('count',$userData['count']);
// 		return $this->fetch('ajaxIndex');

	}
	//编辑
	function userEdit()
	{
		$userModel = new userModel();
		if (request()->isAjax()) {
			(new \app\admin\validate\User()) -> goCheck();
			$data = $userModel->disposeData(input('post.'));
			$userid = input('post.userid');
//			dump(input('post.'));
//			dump($data);exit;
			if($userid > 0){
				$data['expire_time'] = time();
				$res = Db::name('user')->where("userid",$userid)->update($data);
				if ($res !== false) {
					exitjson(1, '保存成功');
				} else {
					exitjson(0, '保存失败');
				}
			}else{
				$user = Db::name('user')->where('user_name',input('post.user_name'))->find();
				if($user){
					exitjson(0, '学号已存在');
				}else{
					$res = Db::name('user')->insertGetId($data);
					if ($res) {
						exitjson(1, '保存成功');
					} else {
						exitjson(0, '保存失败');
					}
				}
			}

		} else {
			if(input('id') > 0){
				$one = Db::name('user')->find(input('id'));
				$this->assign('one',$one);
			}
//            dump($one);exit;
			return $this->fetch('userEdit');
		}
	}

	function userDel(){
		$id = input('id');
		if($id>0){
			if($id==994 || $id==995){
				exitjson(0,'测试账号，请联系管理员删除');
			}
			$data['is_del'] = 1;
			$data['expire_time'] = time();
			$res = Db::name('user')->where("userid",$id)->update($data);
			if($res !== false){
				exitjson('1','删除成功');
			}else{
				exitjson(0,'删除失败');
			}
		}
	}

	//导入表格
    public function importExcel(){
	    header("content-type:text/html;charset=utf-8");
	    if (empty ($_FILES ['excelfile'] ['name'])){
	    	exitjson(0,'请选择文件');
	    }
	    $file_types = explode(".", $_FILES ['excelfile'] ['name']);
		$file_type = $file_types [count($file_types) - 1];
		// dump($file_type);
		/*判别是不是.xls文件，判别是不是excel文件*/
		if (strtolower($file_type) != "xlsx" && strtolower($file_type) !="xls") {
			// $this->error('不是Excel文件，重新上传');
			exitjson(0,'不是Excel文件，重新上传');
		}
	    //上传excel文件
	    $file = request()->file('excelfile');
	    //将文件保存到public/uploads目录下面
	    $info = $file->validate(['size'=>1048576,'ext'=>'xls,xlsx'])->move( 'userfiles/files/');
	    if($info){
	        //获取上传到后台的文件名
	        $fileName = $info->getSaveName();
	        //获取文件路径
	        $filePath = Env::get('root_path').'userfiles'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR.$fileName;
	        //获取文件后缀
	        $suffix = $info->getExtension();
	        //判断哪种类型
	        if($suffix=="xlsx"){
	            $reader = \PHPExcel_IOFactory::createReader('Excel2007');
	            }else{
	            $reader = PHPExcel_IOFactory::createReader('Excel5'); 
	            }
	        }else{
	            // $this->error('文件过大或格式不正确导致上传失败-_-!');
	            exitjson(0,'文件过大或格式不正确导致上传失败-_-!');
	        }
	    //载入excel文件
	    $excel = $reader->load("$filePath",$encode = 'utf-8');
	    //读取第一张表
	    $sheet = $excel->getSheet(0);
	    //获取总行数
	    $row_num = $sheet->getHighestRow();
	    //获取总列数
	    $col_num = $sheet->getHighestColumn();
	    $data = []; //数组形式获取表格数据
	    for ($i=2; $i <=$row_num; $i++) { 
	    	$artType = $sheet->getCell("A".$i)->getValue();
	    	if($artType=='理科' || $artType==2){
	    		$art_type = 2;
	    	}else{
	    		$art_type = 1;
	    	}
	        $data[$i]['art_type']   = $art_type;
	        $data[$i]['user_name'] = $sheet->getCell("B".$i)->getValue();
	        $data[$i]['pwd']     = $excel->getActiveSheet()->getCell("C".$i)->getValue();
	        $data[$i]['password'] = encrypt($data[$i]['pwd']);
	        $data[$i]['nick_name']     = $excel->getActiveSheet()->getCell("D".$i)->getValue();
	        $data[$i]['sex'] = $sheet->getCell("E".$i)->getValue();
	        $data[$i]['add_time'] = time();
	        $data[$i]['is_del'] = 0;
	        //将数据保存到数据库
			$user = Db::name('user')->where('user_name',$data[$i]['user_name'])->find();
			if($user){
				$username = $data[$i]['user_name'];
				unset($data[$i]['user_name']);
				Db::name('user')->where('user_name',$username)->update($data[$i]);
				unset($data[$i]);  //如果学员账号存在，unset掉
			}
	    }
	    // dump($data);exit;
	    $res = Db::name('user')->insertAll($data);
	    if($res){
//			unlink($filePath);
	    	exitjson(1,'导入成功');
	    }else{
	    	exitjson(0,'导入失败');
	    }

	}
















}